package com.chen.cloud.micro.consumer.movie.fallback;

import com.chen.cloud.micro.consumer.movie.api.UserFeignClient;
import com.chen.cloud.micro.consumer.movie.dto.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author ChenTian
 * @date 2022/6/7
 */
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
    private static final Logger logger = LoggerFactory.getLogger(UserFeignClientFallbackFactory.class);

    @Override
    public UserFeignClient create(Throwable throwable) {
        return new UserFeignClient() {
            @Override
            public User findById(Long id) {
                // 日志最好放在各个 fallback 方法中，而不要直接放在 create 方法中，否则在应用启动时，就会打印该日志
                logger.info("fallback; reason was:", throwable);
                User user = new User();
                user.setId(-1L);
                user.setUsername("默认用户");
                return user;
            }

            @Override
            public User create(User user) {
                logger.info("fallback; reason was:", throwable);
                user.setId(-1L);
                return user;
            }
        };
    }
}
